angular.module('FlowCharge.directive', ['FlowCharge.service']);




/*上傳照片指令*/
angular.module('FlowCharge.directive').directive('photoUpload', ['DtoService', function (dto) {
        return {
            restrict: 'E',
            require: '^ngModel',
            replace: true,
            template: '  \
      <input type="file" multiple="false">',
            link: function ($scope, element, attr, ngModel) {
                //var opts = angular.extend({}, $scope.$eval(attrs.nlUploadify)); //继承属性
                element.attr('id', attr.id);
                element.attr('name', attr.name);

                element.uploadify({
                    'formData': {
                        'timestamp': '1483494429',
                        'token': 'e06d9a4aa401ec19cd34c22801e004ab'
                    },
                    'removeTimeout': 1,
                    'fileTypeExts': '*.gif; *.jpg; *.png',
                    'buttonImage': attr.buttonimage, //'image/photo1.jpg',
                    'buttonClass': 'uploadify-class',
                    'height': 110,
                    'width': 110,
                    'swf': 'js/uploadify.swf',
                    'uploader': 'uploadify.php',
                    'itemTemplate': '<div id="${fileID}" class="uploadify-queue-item">\
        <div class="uploadify-progress">\
        <div class="uploadify-progress-bar"><!--Progress Bar--></div>\
          </div>\
            </div>',
                    'onUploadSuccess': function (file, data, response) {
                        //console.log(file,data,response);

                        data = $.parseJSON(data);
                        data.smallFileName = "uploads/" + data.smallFileName;
                        ngModel.$setViewValue(data.fileName);
                        angular.element("#" + attr.id + '-button').css("background-image", "url(" + data.smallFileName + ")");

                        //$("#file").val(data.fileName);
                        //$('#file_upload1-button').css("background-image","url("+data.smallFileName+")");
                    }
                });

                angular.element('.swfupload').css('left', "0px");
                console.log($scope.vo);



            }

        }


    }])




/*卡號添加器*/

angular.module('FlowCharge.directive').directive('cardNumInput', ['AreaService', function (areaService) {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                cardnum: "=cardnum"
                        //increase:"&"

            },
            template: '<div class="m_bottom clearfix"  ng-repeat="item in cardnum track by $index">  \
    <input  type="text" name="card-num[]" ng-model="item" class="inputs fl" placeholder="请添加您购买的流量卡号" />     \
    <a href="javascript:;"  class="j_btn fl marginl10 plus">\
    <img src="image/jia.png" ng-if="$index == 0"  class="increase" />   <img src="image/jian.png" ng-if="$index > 0" class="decrease"  /></a></div>\
    ',
            link: function ($scope, element, attr, ngModel) {

                element.parent().delegate(".decrease", "click", function () {
                    $scope.cardnum.pop();
                    $scope.$apply();

                });
                element.parent().delegate(".increase", "click", function () {
                    $scope.cardnum.push("");

                    $scope.$apply();
                })



                // var ngModel = element.controller('ngModel');

                //  ngModel.$setViewValue("hello");
                //console.log($scope.decrease());
                // var item_tpl = '<div class="m_bottom clearfix">\
                //         <input type="text" class="inputs fl" name="card-num[]" placeholder="请添加您购买的流量卡号" />\
                //         <a href="javascript:;"  class="j_btn fl marginl10 decrease"><img src="image/jian.png" />\
                //         </a></div>';
                //
                // element.find('.plus').click(function(){
                //     angular.element(item_tpl ).appendTo(element.parent());
                // })
                // element.parent().delegate(".decrease","click",function(){
                //        angular.element(this).parent().remove();
                //   });


            }

        }

    }])



/*城市选择器*/
angular.module('FlowCharge.directive').directive('areaSelect', ['AreaService', function (areaService) {
// $scope.area = [];
//  $scope.city = [];
//  $scope.district = [];


        return {
            restrict: 'E',
            //replace: true,

            scope: {
                address: "=address"
            },
            controller: function ($scope, $element, $routeParams, $http) {
                areaService.getAreaJson().then(function (response) {

                    $scope.area = response.data;
                });

            },
            template: '<select class="select1"  ng-model="address.provinceId" id="area-province">\
        <option value="0" selected="selected">请选择</option>\
        <option ng-repeat="(index,item) in area" value="{{index}}">{{item.name}}</option>\
    </select>\
    <label class="label">*</label>\
    <select class="select1" id="area-city" ng-model="address.cityId">\
        <option value="0">请选择</option>\
        <option ng-repeat="(index,item) in city" value="{{index}}">{{item.name}}</option>\
    </select>\
    <label class="label">*</label>\
    <select class="select1" id="area-district" ng-model="address.districtId">\
        <option value="0">请选择</option>\
        <option ng-repeat="(index,item) in district" value="{{index}}">{{item.name}}</option>\
    </select>',
            link: function ($scope, element, attr) {
                $scope.address.districtId = "0";
                $scope.address.districtName = "";
                $scope.address.provinceId = "0";
                $scope.address.provinceName = "";
                $scope.address.cityId = "0";
                $scope.address.cityName = "";





//  $scope.city ={'001':{"name":"xxx"}};
                element.find('#area-province').change(function () {

                    //  console.log(angular.element(this).val());
                    if (angular.element(this).val() > 0) {
                        $scope.address.cityId="0";
                        $scope.address.districtId="0";
                        $scope.city = $scope.area[$(this).val()]['city'];
                        $scope.address.provinceName = $scope.area[$(this).val()]['name'];
                       console.log( $scope.address);
                        $scope.$apply();
                    } else {
                        // alert("请选择城市");
                    }


                });
                element.find('#area-city').change(function () {

                    //  console.log(angular.element(this).val());
                    if (angular.element(this).val() > 0) {
                        $scope.district = $scope.area[$("#area-province").val()]['city'][$(this).val()]['district'];
                         $scope.address.cityName =$scope.area[$("#area-province").val()]['city'][$(this).val()]['name']
                        $scope.$apply();
                    } else {
                        // alert("请选择城市");
                    }


                });
                
                 element.find('#area-district').change(function () {

                    //  console.log(angular.element(this).val());
                    if (angular.element(this).val() > 0) {
                       
                         $scope.address.districtName  = $scope.area[$("#area-province").val()]['city'][$('#area-city').val()]['district'][$(this).val()]['name'];
                        $scope.$apply();
                    } else {
                        // alert("请选择城市");
                    }


                });



                // element.change(function(){
                //   if (angular.element(this).val() > 0) {
                //       $scope.city  = $scope.area[$(this).val()]['city'];
                //
                //   } else {
                //       // alert("请选择城市");
                //   }
                //
                // });




            }


        }
    }])






/*历史选择器*/
angular.module('FlowCharge.directive').directive('comboSelect', ['$timeout', 'HistorySerivce', function ($timeout, history) {


        return {
            restrict: 'E',
            replace: true,
            scope: {
                phone: "=phone"
            },
            controller: function ($scope, $element, $routeParams, $http) {
                //console.log($scope.phone=123);
                //$scope.month_meals = [1,2,3];
            },
            template: '<select ng-model="phone" >\
            <option value="">请输入卡号</option>\
              <option value="{{history}}" ng-repeat="history in historyList">{{history}}</option>\
          </select>',
            link: function ($scope, element, attr) {
                //初始化cookie
                //$cookie.get("sdf");

                $scope.historyList = history.getHistory();
                $scope.getSelectVal = function () {
                    return angular.element(".combo-input").val();
                }



                $timeout(function () {


                    element.comboSelect();//渲染后在实例化组件

                    //
                    angular.element(".combo-input").keyup(function () { //四位数字隔开
                        var value = angular.element(this).val().replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");
                        angular.element(this).val(value);
                        $scope.phone = value;
                        $scope.$apply();
                    })
                }, 0)
                // alert(element.find('option').length);
            }
        };


    }]);
